import {
  call, put, all, takeLatest, select
} from 'redux-saga/effects'
import api from '@API'
import {
  GET_TAG_DETAIL,
  GET_TAG_SURVEYS,
  GET_TAG_FEED_LIST,
  GET_TAG_DOCTORS,
  GET_TAG_VIDEOS,
  GET_TAG_FEATURED,
  GET_TAG_PLAN,
  GET_TAG_GOODS,
} from '@ACTIONS/tag'
import { getTagFeedData } from '@SAGAS/dataSelect'
import { DEFAULT_TAKE } from '@CONSTANTS/common';
import { DECISION_TYPE } from '@CONSTANTS/decision'

const getTagDetail = function* getTagDetail(action) {
  const { payload: { tId } } = action
  try {
    const data = yield call(api.getTagById, { tId })
    yield put({ type: GET_TAG_DETAIL.SUCCESS, data })
    yield put({ type: GET_TAG_SURVEYS.REQUEST, tId })
    yield put({ type: GET_TAG_FEATURED.REQUEST, tId })
    yield put({ type: GET_TAG_DOCTORS.REQUEST, tId })
    yield put({ type: GET_TAG_VIDEOS.REQUEST, tId })
    yield put({ type: GET_TAG_PLAN.REQUEST, tId, data })
    yield put({ type: GET_TAG_GOODS.REQUEST, tId, data })
  } catch (error) {
    yield put({ type: GET_TAG_DETAIL.FAILURE })
  }
}

const getTagSurveys = function* getTagSurveys(action) {
  try {
    const { tId } = action
    const { DecisionFlows, SurveyTemplates } = yield call(api.getSurveyTemplateAndDeceisionFlowByTag, { tId, skip: 0, take: 3 })
    let data = []
    if (Array.isArray(DecisionFlows)) {
      data = [...DecisionFlows.map(tmp => ({ ...tmp, DecisionType: DECISION_TYPE.Decision }))]
    }
    if (Array.isArray(SurveyTemplates)) {
      data = [...data, ...SurveyTemplates.map(tmp => ({ ...tmp, DecisionType: DECISION_TYPE.Survey }))]
    }
    yield put({ type: GET_TAG_SURVEYS.SUCCESS, data: data.slice(0, 3) })
  } catch (error) {
    yield put({ type: GET_TAG_SURVEYS.FAILURE })
  }
}
const getTagFeatured = function* getTagFeatured(action) {
  try {
    const { tId } = action
    const { FeedItems } = yield call(api.getNewsFeedByTag, {
      tId, skip: 0, take: 4, protected: true
    })
    yield put({ type: GET_TAG_FEATURED.SUCCESS, data: FeedItems })
  } catch (error) {
    yield put({ type: GET_TAG_FEATURED.FAILURE })
  }
}
const getTagDoctors = function* getTagDoctors(action) {
  try {
    const { tId } = action
    const { Doctors, Total } = yield call(api.getRecommendDoctorsTopic, {
      tId, skip: 0, take: 3, padding: true
    })
    yield put({ type: GET_TAG_DOCTORS.SUCCESS, data: Doctors, tagDoctorMore: Total > 3 })
  } catch (error) {
    yield put({ type: GET_TAG_DOCTORS.FAILURE })
  }
}
const getTagVideos = function* getTagVideos(action) {
  try {
    const { tId } = action
    const { Courses } = yield call(api.getCoursesByTag, { tId, skip: 0, take: 5 })
    yield put({ type: GET_TAG_VIDEOS.SUCCESS, data: Courses })
  } catch (error) {
    yield put({ type: GET_TAG_VIDEOS.FAILURE })
  }
}
const getTagPlan = function* getTagPlan(action) {
  try {
    const { tId, data } = action
    const { RelatedTags = [] } = data || {}
    let TagIds = [tId]
    const tagArray = RelatedTags?.map(item => item.TagId)
    Array.isArray(tagArray) && (TagIds = [...TagIds, ...tagArray])
    const { Templates } = yield call(api.getTemplatesByTagIds, { tIds: JSON.stringify(TagIds) })
    yield put({ type: GET_TAG_PLAN.SUCCESS, data: Templates })
  } catch (error) {
    yield put({ type: GET_TAG_PLAN.FAILURE })
  }
}

const getTagFeedList = function* getTagFeedList(action) {
  try {
    const { payload = {} } = action
    const { skip, tId } = payload
    const preList = yield select(getTagFeedData)
    const realSkip = skip === 0 ? 0 : preList.length
    const { FeedItems } = yield call(api.getNewsFeedByTag, {
      skip: realSkip,
      tId,
      protected: false,
      take: DEFAULT_TAKE
    })
    yield put({
      type: GET_TAG_FEED_LIST.SUCCESS,
      data: skip === 0 ? FeedItems : [...preList, ...FeedItems],
      canLoadMore: FeedItems.length >= DEFAULT_TAKE
    })
  } catch (error) {
    yield put({ type: GET_TAG_FEED_LIST.FAILURE, error })
  }
}

const getTagGoods = function* getTagGoods(action) {
  try {
    const { tId, data } = action
    const { RelatedTags = [] } = data || {}
    let TagIds = [tId]
    const tagArray = RelatedTags?.map(item => item.TagId)
    Array.isArray(tagArray) && (TagIds = [...TagIds, ...tagArray])
    const { GoodsList } = yield call(api.getGoodsByTagIds, { tIds: JSON.stringify(TagIds) })
    yield put({ type: GET_TAG_GOODS.SUCCESS, data: GoodsList })
  } catch (error) {
    yield put({ type: GET_TAG_GOODS.FAILURE })
  }
}

export default function* rootSaga() {
  yield all([
    takeLatest(GET_TAG_DETAIL.REQUEST, getTagDetail),
    takeLatest(GET_TAG_SURVEYS.REQUEST, getTagSurveys),
    takeLatest(GET_TAG_FEATURED.REQUEST, getTagFeatured),
    takeLatest(GET_TAG_DOCTORS.REQUEST, getTagDoctors),
    takeLatest(GET_TAG_VIDEOS.REQUEST, getTagVideos),
    takeLatest(GET_TAG_PLAN.REQUEST, getTagPlan),
    takeLatest(GET_TAG_FEED_LIST.REQUEST, getTagFeedList),
    takeLatest(GET_TAG_GOODS.REQUEST, getTagGoods),
  ]);
}

